// 引入axios
import axios from 'axios'

// import Vue from 'vue'
import { getToken, removeToken } from './auth.js'
import { ElMessage } from 'element-plus'
const baseURL = ''
const fileURL = ''
// 创建axios实例
const httpService = axios.create({
  baseURL: baseURL
  // 请求超时时间
  // timeout: 30000 // 需自定义
})

// request拦截器
httpService.interceptors.request.use(
  (config) => {
    const token = getToken()
    if (token !== null && token !== undefined) {
      config.headers.Authorization = 'Bearer ' + token
    }
    return config
  },
  (error) => {
    // 请求错误处理
    Promise.reject(error)
  }
)

// respone拦截器
httpService.interceptors.response.use(
  (response) => {
    // 统一处理状态;
    const res = response.data
    return res
  },
  // 处理处理
  (error) => {
    if (error && error.response) {
      switch (error.response.status) {
        case 400:
          error.message = error.response.data.message
          break
        case 401:
          error.message = '未授权或授权过期，请重新登录'
          removeToken()
          break
        case 403:
          error.message = '拒绝访问'
          break
        case 404:
          error.message = '请求错误,未找到该资源'
          break
        case 405:
          error.message = '请求方法未允许'
          break
        case 408:
          error.message = '请求超时'
          break
        case 500:
          error.message = '服务器端出错'
          break
        case 501:
          error.message = '网络未实现'
          break
        case 502:
          error.message = '网络错误'
          break
        case 503:
          error.message = '服务不可用'
          break
        case 504:
          error.message = '网络超时'
          break
        case 505:
          error.message = 'http版本不支持该请求'
          break
        default:
          error.message = `未知错误${error.response.status}`
      }
      if (error.response.data.message) {
        error.message = error.response.data.message
      }
    } else {
      error.message = '连接到服务器失败'
    }
    ElMessage.error(error.message)
    return Promise.reject(error)
  }
)

/* 网络请求部分 */

/*
 *  get请求
 *  url:请求地址
 *  params:参数
 * */
export function get(url, params = {}) {
  return new Promise((resolve, reject) => {
    httpService({
      url: url,
      method: 'get',
      params: params
    })
      .then((response) => {
        resolve(response)
      })
      .catch((error) => {
        reject(error)
      })
  })
}

/*
 *  post请求
 *  url:请求地址
 *  params:参数
 * */
export function post(url, data = {}, params = {}) {
  return new Promise((resolve, reject) => {
    httpService({
      url: url,
      method: 'post',
      data: data,
      params
    })
      .then((response) => {
        resolve(response)
      })
      .catch((error) => {
        reject(error)
      })
  })
}

/*
 *  put请求
 *  url:请求地址
 *  data:参数
 * */
export function put(url, data = {}, params) {
  return new Promise((resolve, reject) => {
    httpService({
      url: url,
      method: 'put',
      data: data,
      params
    })
      .then((response) => {
        resolve(response)
      })
      .catch((error) => {
        reject(error)
      })
  })
}

/*
 *  delete请求
 *  url:请求地址
 *  data:参数
 * */
export function deleteRequest(url, params = {}, data = {}) {
  return new Promise((resolve, reject) => {
    httpService({
      url: url,
      method: 'delete',
      params: params,
      data
    })
      .then((response) => {
        resolve(response)
      })
      .catch((error) => {
        reject(error)
      })
  })
}
/*
 *  文件上传
 *  url:请求地址
 *  data:数据
 *  param:参数
 * */
export function fileUpload(url, data = {}, param) {
  return new Promise((resolve, reject) => {
    httpService({
      url: url,
      method: 'post',
      params: param, // 参数
      data: data, // 数据
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
      .then((response) => {
        resolve(response)
      })
      .catch((error) => {
        reject(error)
      })
  })
}

/*
 *  文件流下载  post请求
 *  url:请求地址
 *  params:数据
 *  param:参数
 * */
export function fileDownload(url, params) {
  return new Promise((resolve, reject) => {
    httpService({
      url: url,
      method: 'post',
      data: params, // 数据
      responseType: 'blob'
    })
      .then((response) => {
        resolve(response)
      })
      .catch((error) => {
        reject(error)
      })
  })
}

/**
 * 文件流下载  get请求
 * 请求地址
 * @param {*} url
 * @param {*} params
 */
export function fileDownloadGet(url, params) {
  return new Promise((resolve, reject) => {
    httpService({
      url: url,
      method: 'get',
      params: params, // 数据
      responseType: 'blob'
    })
      .then((response) => {
        resolve(response)
      })
      .catch((error) => {
        reject(error)
      })
  })
}

export default {
  baseURL,
  fileURL,
  get,
  post,
  put,
  deleteRequest,
  fileUpload,
  fileDownload,
  fileDownloadGet
}
